package com.mlnx.work.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mlnx.common.annotations.NeedLogin;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.work.config.ControllerNames;
import com.mlnx.work.config.RightKeys;
import com.mlnx.work.entity.User;
import com.mlnx.work.pojo.form.UserFilterForm;
import com.mlnx.work.pojo.form.UserForm;
import com.mlnx.work.service.UserService;

import org.shan.spring.base.BaseController;
import org.shan.spring.base.annotations.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by amanda.shan on 2020/5/19.
 */
@Api(value = ControllerNames.AdminName + "接口", tags = {ControllerNames.AdminName + "接口"})
@SysLog(title = ControllerNames.AdminName)
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    private UserService userService;

    @NeedLogin
    @ApiOperation(
            value = "分页获取",
            notes = ""
    )
    @GetMapping({""})
    public ResponseData<IPage> listPage(@Valid PageForm pageForm, UserFilterForm ft) throws IllegalAccessException {
        return this.result(userService.page(pageForm, ft));
    }

    @NeedLogin
    @ApiOperation(
            value = "获取所有",
            notes = ""
    )
    @GetMapping({"/all"})
    public ResponseData<List<User>> listAll(UserFilterForm ft) throws IllegalAccessException {
        return this.result(userService.listAll(ft));
    }


    @NeedLogin(permissions = {RightKeys.RIGHT_USER_MANAGER, RightKeys.RIGHT_REVIEW})
    @ApiOperation(
            value = "更改",
            notes = ""
    )
    @PutMapping({"/{id}"})
    public Response modify(@PathVariable("id") Integer id, @Valid  @RequestBody UserForm form) {
        userService.updateById(id, form);
        return this.result();
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_USER_MANAGER})
    @ApiOperation(
            value = "删除",
            notes = ""
    )
    @DeleteMapping({"/{id}"})
    public Response delete(@PathVariable("id") Integer id) {
        userService.removeById(id);
        return this.result();
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_USER_MANAGER})
    @ApiOperation(
            value = "批量删除",
            notes = ""
    )
    @DeleteMapping({""})
    public Response deletes(Integer[] ids) {
        userService.removeByIds(Arrays.asList(ids));
        return this.result();
    }


}
